package com.ejuornal.service.impl;

import javassist.expr.NewArray;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Admin;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.AdminDao;
import com.ejuornal.service.AdminService;
import com.ejuornal.util.MD5;
/**
 * 项目名称：ejuornal    
 * 类名称：AdminServiceImpl    
 * 类描述：管理员信息业务逻辑层实现类    
 * 创建人：陈晨    
 * 创建时间：Jan 26, 2013 4:42:34 AM    
 * 修改人：陈晨    
 * 修改时间：Jan 26, 2013 4:42:34 AM    
 * 修改备注：    
 * @version     
 */
@Component("adminService")
public class AdminServiceImpl implements AdminService{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
    private AdminDao adminDao;
    
    public Admin login(Admin admin) {
       admin = adminDao.validate(admin);
       logger.debug("用户登录");
        return admin;
    }
    
    public void add(Admin admin){
        String sql="insert into t_admin(account, password,roleId) value(?, ?, ?)";
        adminDao.add(sql, new Object[]{admin.getAccount(),MD5.MD5Encode( admin.getPassword()), admin.getRoleId()});
    }

    public boolean resetPwd(Admin admin, String newPwd) {
        admin.setPassword(MD5.MD5Encode(admin.getPassword()));
        newPwd = MD5.MD5Encode(newPwd);
        return adminDao.resetPwd(admin, newPwd);
    }
    
    public void delete(int... adminIds)
    {
    	String sql = "delete from t_admin where adminId = ?";
    	adminDao.batchDelete(sql, adminIds);
    }
    
    public   Admin  getAdminById(int   adminId){
    	Admin  admin = adminDao.getAdminById(adminId);
    	return  admin;
    	
    }
    
    public Page<Admin> findAllAdmin(int current_record, String url ,int roleId) {
		// TODO Auto-generated method stub
    	System.out.println(roleId);
    	Page<Admin>  page=null;
    	if(roleId !=0){
    	String   hsql="where roleId = ?";
    	 page=adminDao.find(current_record, url, hsql, new Object[]{roleId});
    	}else {
			page=adminDao.find(current_record, url);
		}
		return page;
	}
    
    public AdminDao getAdminDao() {
        return adminDao;
    }
    @Resource(name="adminDao")
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

	
}
